package cn.cc.goods.service.serviceImpl;

import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.result.utils.RestResult;
import cn.cc.common.result.utils.RestResultEmum;
import cn.cc.goods.mapper.mapper.SmallMiGoodsSpuMapper;
import cn.cc.goods.mapper.pojo.SmallMiGoodsSpu;
import cn.cc.goods.service.GoodsSpuService;
import cn.cc.goods.service.bo.AddGoodsSpuBO;
import cn.cc.goods.service.bo.DelGoodsSpuBO;
import cn.cc.goods.service.bo.UpdGoodsSpuBO;
import cn.cc.goods.service.domain.GetGoodsSpuDO;
import cn.cc.goods.service.domain.GetSpuByGoodsIdDO;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: smail-mi-cloud
 * @description: 商品sku           service
 * @author: Mr.WuChao
 * @create: 2021-07-16 14:32
 **/
@Service
public class GoodsSpuServiceImpl implements GoodsSpuService {
    @Autowired
    private SmallMiGoodsSpuMapper spuMapper;

    @Override
    public RestResult<Void> addGoodsSpu(AddGoodsSpuBO spuBO) {
        SmallMiGoodsSpu goodsSpu = BeanCopierUtil.copyObj(spuBO, SmallMiGoodsSpu.class);
        //自动生成id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String id = objectIdGenerator.next();
        goodsSpu.setGoodsId(id);//商品id
        goodsSpu.setCreatedBy("cc");
        goodsSpu.setCreationDate(new Date());
        goodsSpu.setModifyDate(new Date());
        goodsSpu.setVersion("1");
        int i = spuMapper.insertSelective(goodsSpu);
        if (i == 1) {
            return new RestResult<>(RestResultEmum.SUCCESS.code, RestResultEmum.SUCCESS.msg);
        }
        return new RestResult<>(RestResultEmum.SAVE_ERROR.code, RestResultEmum.SAVE_ERROR.msg);
    }

    @Override
    public RestResult<List<GetGoodsSpuDO>> getGoodsSpu() {
        List<SmallMiGoodsSpu> goodsSpus = spuMapper.selectAll();
        List<GetGoodsSpuDO> dos = BeanCopierUtil.copyList(goodsSpus, GetGoodsSpuDO.class);
        if (dos == null || dos.size() == 0) {
            return new RestResult<>(RestResultEmum.GET_ERROR.code, RestResultEmum.GET_ERROR.msg);
        }
        return new RestResult<>(dos);
    }


    @Override
    public RestResult<Void> delGoodsSpu(DelGoodsSpuBO delGoodsSpuBO) {
        SmallMiGoodsSpu goodsSpu = BeanCopierUtil.copyObj(delGoodsSpuBO, SmallMiGoodsSpu.class);
        int delete = spuMapper.delete(goodsSpu);
        if (delete == 0) {
            return new RestResult<>(RestResultEmum.DEL_ERROR.code, RestResultEmum.DEL_ERROR.msg);
        }
        return new RestResult<>();
    }

    @Override
    public RestResult<Void> updGoodsSpu(UpdGoodsSpuBO updGoodsSpuBO) {
        SmallMiGoodsSpu goodsSpu = BeanCopierUtil.copyObj(updGoodsSpuBO, SmallMiGoodsSpu.class);
        int update = spuMapper.updateByPrimaryKey(goodsSpu);
        if (update == 0) {
            return new RestResult<>(RestResultEmum.EDIT_ERROR.code, RestResultEmum.EDIT_ERROR.msg);
        }
        return new RestResult<>();
    }

    @Override
    public RestResult<Void> addGoodSpuOne(AddGoodsSpuBO addGoodsSpuBO) {
        SmallMiGoodsSpu goodsSpu = BeanCopierUtil.copyObj(addGoodsSpuBO, SmallMiGoodsSpu.class);
        int insertSelective = spuMapper.insertSelective(goodsSpu);
        if (insertSelective == 0) {
            return new RestResult<>(RestResultEmum.SAVE_ERROR.code, RestResultEmum.SAVE_ERROR.msg);
        }
        return new RestResult<>();
    }
}
